const router = require('koa-router')()

//获取全部帮助
router.get('/gethelp', async (ctx, next) => {
    let sql = "select * from help";
    let result = await ctx.db.EXCUTE(sql);
    ctx.body = { data: result, status: "获取信息成功" }
})

//查询某个帮助
router.post('/selhelp', async (ctx, next) => {
    let question = ctx.request.body.question
    let sql = `select * from help where question like '%${question}%'`;
    let result = await ctx.db.EXCUTE(sql);
    if (result == "") {
        ctx.body = { status: "没有该帮助及其相似帮助的信息，请重新输入" }
    } else {
        ctx.body = { data: result, status: "获取信息成功" }
    }
})

// 上传帮助
router.post('/addhelp', async function (ctx, next) {
    let res = ctx.request.body
    let sql = `insert into help(question,answer) values (?,?)`
    await ctx.db.EXCUTE(sql, [res.question, res.answer]);
    ctx.body = { status: "增加帮助信息成功" }
})

// 修改帮助信息
router.post('/edithelp', async function (ctx, next) {
    let res = ctx.request.body
    let sql = `update help set question=?,answer=? where id = ?`;
    await ctx.db.EXCUTE(sql, [res.question, res.answer, res.id]);
    ctx.body = { status: "修改帮助信息成功" }
})

// 删除帮助信息
router.post('/delhelp', async function (ctx, next) {
    let id = ctx.request.body.id;
    let sql = `delete from help where id = "${id}"`
    await ctx.db.EXCUTE(sql);
    ctx.body = { status: "删除帮助信息成功" }
})

module.exports = router